package com.example.demo1;

import java.io.*;
import java.util.Enumeration;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello World!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer=response.getWriter();
        //通过request获取HTTP请求中的请求方式
        writer.println("getMethod:"+request.getMethod());
        writer.println("<br/>");
        //通过request获取HTTP请求中的请求URI
        writer.println("getRequestURI:"+request.getRequestURI());
        writer.println("<br/>");
        writer.println("getProtocol:"+request.getProtocol());
        writer.println("<br/>");
        //通过request获取HTTP请求中的应用程序路径
        writer.println("getContextPath:"+request.getContextPath());

        writer.println("<br/>");
        writer.println("getPathInfo:"+request.getPathInfo());
        writer.println("<br/>");
        writer.println("getServletPath:"+request.getServletPath());
        //获取包含所有请求字段的Enumeration对象headerNames
        Enumeration headerNames=request.getHeaderNames();

        while(headerNames.hasMoreElements()){
            String headerName= (String) headerNames.nextElement();
            writer.println(headerName+":"+request.getHeader(headerName)+"<br/>");
        }
    }

    public void destroy() {

    }
}